package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.PushRecord;
import com.example.prison.service.PushRecordService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("推送记录")
@RestController
@RequestMapping("/api")
public class PushRecordController {

    @Autowired
    private PushRecordService pushRecordService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取推送记录列表")
    @GetMapping("/getPushRecord")
    public Response<PageInfo<PushRecord>> getPushRecord(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取推送记录列表");
        PageInfo<PushRecord> pageInfo = pushRecordService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增推送记录")
    @PostMapping("/addPushRecord")
    public Response<?> addPushRecord(@RequestBody PushRecord record) {
        log.info("新增推送记录");
        systemLogService.add("新增推送记录");
        int result = pushRecordService.add(record);
        return Response.success("添加成功！");
    }

    @ApiOperation("依据id获取推送记录列表")
    @GetMapping("/getPushRecordById")
    public Response<PushRecord> getPushRecordById(@Parameter(description = "推送记录ID")@RequestParam(value = "id") Integer id) {
        log.info("依据id获取推送记录列表");
        systemLogService.add("依据id获取推送记录列表");
        PushRecord record = pushRecordService.getById(id);
        if(ObjectUtils.isEmpty(record)) {
            return Response.error("此推送记录id不存在！");
        }
        return Response.success("获取成功！", record);
    }

    @ApiOperation("依据推送人获取推送记录")
    @GetMapping("/getPushRecordByPushPersonnel")
    public Response<PageInfo<PushRecord>> getPushRecordByPushPersonnel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                            @Parameter(description = "推送人")@RequestParam(value = "pushPersonnel") String pushPersonnel) {
        log.info("依据推送人获取推送记录");
        systemLogService.add("依据推送人获取推送记录");
        PageInfo<PushRecord> pageInfo = pushRecordService.getByPushPersonnel(pageNumber, pageSize, pushPersonnel);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此推送记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据时间获取推送记录")
    @GetMapping("/getPushRecordByTime")
    public Response<PageInfo<PushRecord>> getPushRecordByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                @Parameter(description = "开始时间")@RequestParam(value = "beginTime") String beginTime,
                                                                @Parameter(description = "结束时间")@RequestParam(value = "endTime") String endTime) {
        log.info("依据时间获取推送记录");
        systemLogService.add("依据时间获取推送记录");
        if(beginTime.compareTo(endTime) > 0) {
            return Response.error("时间顺序错误，请重新输入！");
        }
        PageInfo<PushRecord> pageInfo = pushRecordService.getByTime(pageNumber, pageSize, beginTime, endTime);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("该时间内推送记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据报警等级获取推送记录")
    @GetMapping("/getPushRecordByLevel")
    public Response<PageInfo<PushRecord>> getPushRecordByLevel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                           @Parameter(description = "报警等级")@RequestParam(value = "alarmLevel") String alarmLevel) {
        log.info("依据报警等级获取推送记录");
        systemLogService.add("依据报警等级获取推送记录");
        PageInfo<PushRecord> pageInfo = pushRecordService.getByLevel(pageNumber, pageSize, alarmLevel);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此推送记录不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
